package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.ResearchReportDao;
import com.ewili.x.special.entity.ResearchReport;
import com.ewili.x.special.service.data.inter.ResearchReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author wonder
 */
@Service
public class ResearchReportServiceImpl implements ResearchReportService {

    @Autowired
    private ResearchReportDao researchReportDao;

    @Override
    public void spideResearchReport() {
        String baseUrl = "http://datainterface.eastmoney.com//EM_DataCenter/js.aspx?type=SR&sty=GGSR&js=var%20hklnNyFS={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22,%22count%22:%22(count)%22}&ps=100&p=1&mkt=1&stat=0&cmd=4&code=&rt=51230904";
        JSONObject jsonObject = ObjectHttpClientUtils.formatEastMoneyJson(baseUrl, "var hklnNyFS=", 3000000);
        Integer pages = jsonObject.getInteger("pages");
        for (int i = 1; i <= pages; i++) {
            String url = "http://datainterface.eastmoney.com//EM_DataCenter/js.aspx?type=SR&sty=GGSR&js=var%20hklnNyFS={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22,%22count%22:%22(count)%22}&ps=100&p=" + i + "&mkt=1&stat=0&cmd=4&code=&rt=51230904";
            this.spideResearchReport(url);
        }

    }

    @Override
    public void spideResearchReport(String url) {
        JSONObject jsonObject = ObjectHttpClientUtils.formatEastMoneyJson(url, "var hklnNyFS=", 3000000);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonContent = jsonArray.getJSONObject(i);
            String sratingName = jsonContent.getString("sratingName");
            String insName = jsonContent.getString("insName");
            String author = jsonContent.getString("author");
            String changes = jsonContent.getString("change");
            String title = jsonContent.getString("title");
            Date datetime = jsonContent.getDate("datetime");
            String secuFullCode = jsonContent.getString("secuFullCode");
            secuFullCode = secuFullCode.split("/.")[0].replace(".SZ", "").replace(".SH", "");

            boolean isexists = researchReportDao.existsByCodeAndDateAndAuthor(secuFullCode, datetime, author);

            if (isexists) {
                continue;
            }

            ResearchReport researchReport = new ResearchReport();
            researchReport.setAuthor(author);
            researchReport.setChanges(changes);
            researchReport.setInsName(insName);
            researchReport.setTitle(title);
            researchReport.setSratingName(sratingName);
            researchReport.setCode(secuFullCode);
            researchReport.setDate(datetime);

            researchReportDao.saveAndFlush(researchReport);

        }
    }
}
